var config = require('./config.js')
var base_url = config.base_url;
var chance = require('./chancenet.js')
//console.log(chance)
function Post(url, data, success, failer) {
  var lan = wx.getStorageSync('lang')
  var language = lan == 'en' ? 'en_us' : 'zh_cn'
  var param = data;
  //从本地获取token,这里只做演示写死123
  var access_token = wx.getStorageSync('createUser').access_token;
  let chain_netWork = chance.chain_netWork;
  //let chain_netWork = '';

  //如果token存在
  if ((typeof access_token != "undefined") && access_token != null) {
    Object.assign(param, { 'access_token': access_token});
   // console.log(param)
  }
  Object.assign(param, { 'language': language, chain_network: chain_netWork });
 //console.log(param)chain_network:'main_network' 
  var full_url;
  var temp = url.indexOf("http");
  if (temp == -1) {
    // 表示url不是以http开头；
    full_url = base_url + url;
  } else if (temp == 0) {
    // 表示url是以http开头
    full_url = url;
  } else if (temp > 0) {
    // 假如是大于0 包含该字符串
    console.log('not url')
  }
  // wx.showLoading({
  //   title: language == 'en_us'?"loading...":'加载中...'
  // })
  wx.request({
    method: 'POST',
    url: full_url,
    data: param,
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    success: (wxResponseObj) => {
      typeof success == "function" && success(wxResponseObj.data);
      
      if (wxResponseObj.data.message == 'Unauthorized') {
        wx.showToast({
          title: wxResponseObj.data.message == 'Unauthorized' ? (lan == "en" ? ' Account failure' : "账号失效") : wxResponseObj.data.message,
          icon: 'none',
          duration: 500,
          success:function(){
            wx.reLaunch({
              url: '/pages/lead/leadIndex',
            })
            wx.clearStorage();
          }
        })
      
      }
    },
    fail: (err) => {
      typeof failer == "function" && failer(err);
    }
  });
};

module.exports = {
  post: Post
};